package com.useeinfo.oa.modules.wuHanCommon.base.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.tools.AnalysisObjectList;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.vo.TreeSelectVO;
import com.useeinfo.oa.modules.security.vo.TreeVO;
import com.useeinfo.oa.modules.wuHanCommon.base.dao.WarningGroupInnerUserDao;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WarningGroup;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WarningGroupInnerUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WarningGroupInnerUserBiz extends CrudBiz<WarningGroupInnerUserDao, WarningGroupInnerUser> {

    @Autowired
    private SystemUserBiz systemUserBiz;

    @Autowired
    private WarningGroupBiz warningGroupBiz;

    /**
     * 获取所有报警组人员
     *
     * @param sysCode
     * @param warningGroupId
     * @return
     */
    public ResultDto getWarningGroupInnerUserTree(String sysCode, String warningGroupId) {
        JSONObject data = new JSONObject();
        List<TreeVO> treeVOList = new ArrayList<>();
        List<SystemUser> systemUsers = systemUserBiz.findList();
        if (CollectionUtils.isNotEmpty(systemUsers)) {
            systemUsers.forEach(systemUser -> {
                treeVOList.add(new TreeVO(systemUser.getUserName(), "0",
                        systemUser.getId() + "", systemUser.getUserName(), "module"));
            });
        }
        JSONObject menuMap = new JSONObject();
        menuMap.put(sysCode, treeVOList);
        data.put("menuMap", menuMap);

        //选中项
        data.put("roleMenuList", getSelectUserByWarningGroupId(StringConverters.ToLong(warningGroupId), sysCode));
        return ResultUtil.success(data);
    }

    /**
     * 获取选中的人员
     *
     * @param warningGroupId
     * @return
     */
    public List<TreeSelectVO> getSelectUserByWarningGroupId(Long warningGroupId, String sysCode) {
        List<TreeSelectVO> treeSelectVOS = new ArrayList<>();
        if (warningGroupId != null) {
            List objects = dao.getUserByGroupId(warningGroupId);
            if (CollectionUtils.isNotEmpty(objects)) {
                for (Object object : objects) {
                    net.sf.json.JSONArray j = net.sf.json.JSONArray.fromObject(object);
                    treeSelectVOS.add(new TreeSelectVO(sysCode, AnalysisObjectList.getJsonString(j, 0)));
                }
            }
        }
        return treeSelectVOS;
    }

    /**
     * 保存报警组信息
     *
     * @param warningGroup
     * @param treesData
     */
    public ResultDto saveWarningGroup(WarningGroup warningGroup, List<String> treesData) {
        warningGroupBiz.addOrUpdate(warningGroup);
        //将所有的用户删除，将现在的用户加上
        this.deleteUserByGroupId(warningGroup.getId());
        this.addUserByGroupId(warningGroup, treesData);
        return ResultUtil.success();
    }

    /**
     * 删除报警组的人员
     *
     * @param warningGroupId
     */
    public void deleteUserByGroupId(Long warningGroupId) {
        if (warningGroupId == null) {
            return;
        }
        dao.deleteUserByGroupId(warningGroupId);
    }

    /**
     * 新增报警组的人员
     *
     * @param warningGroup
     * @param treesData
     */
    public void addUserByGroupId(WarningGroup warningGroup, List<String> treesData) {
        if (CollectionUtils.isNotEmpty(treesData)) {
            treesData.forEach(userId -> {
                SystemUser systemUser = systemUserBiz.findModel(StringConverters.ToLong(userId));
                if (systemUser != null)
                    this.addOrUpdate(new WarningGroupInnerUser(warningGroup, systemUser, null));
            });
        }
    }


}
